<?php

namespace App\Console\Commands;

use App\Models\User;
use App\Models\Phone;
use Illuminate\Support\Arr;
use Illuminate\Console\Command;

class CreateData extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'create:data';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '创建数据';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        // 创建手机数据
        $this->createPhoneData();

        // 创建
    }


    protected function createPhoneData()
    {
        $colorArr = ['blue', 'green', 'white', 'black'];
        // 循环用户数据
        User::query()->chunkById(100, function ($items) use ($colorArr) {
            /* @var User $item */
            foreach ($items as $item) {
                $item->phone()->create([
                    'color' => Arr::get($colorArr, mt_rand(0, 3), '灰色'),
                    'price' => mt_rand(100, 999),
                    'number' => mt_rand(1849078, 7849078),
                ]);
            }
        }, 'id');
    }
}
